public class Solution {
    public String longestPalindrome(String s) {
        int n = s.length();
        boolean[][] dp = new boolean[n][n];
        int begin = 0, end = 0;
        for(int i = n - 1; i >= 0; i--){
            for(int j = i; j < n; j++){
                if(s.charAt(i) == s.charAt(j)){
                    dp[i][j] = i + 1 >= j || dp[i+1][j-1]; //去掉i,j位置字符后的子串
                } else {
                    dp[i][j] = false;
                }
                if(dp[i][j] && j - i > end - begin){
                    begin = i;
                    end = j;
                }
            }
        }
        return s.substring(begin,end+1);

    }
}
